import React, { Component } from "react";
import Form from "./Form";
import List from "./List";

class App extends Component {
  state = {
    list: [
      {
        name: "zhangsan",
        id: 1,
      },
      {
        name: "lisi",
        id: 2,
      },
    ],
  };

  add = (v) => {
    this.setState((prevState) => {
      return {
        list: [...prevState.list, { name: v, id: new Date().getTime() }],
      };
    });
  };

  remove = (id) => {
    this.setState((prevState) => {
      return {
        list: prevState.list.filter((item) => item.id !== id),
      };
    });
  };

  render() {
    const { list } = this.state;
    return (
      <>
        <h2>todolist</h2>
        <Form onGetItem={this.add} />
        <List list={list} onRemove={this.remove} />
      </>
    );
  }
}

export default App;
